<template>
	<view class="center">
		<view class="u-flex user-box u-p-l-30 u-p-r-20 u-p-b-30" @click="toLogin">
			<view class="u-m-r-10">
				<u-avatar :src="userInfo.avatarUrl&&userDetail.userName ? userInfo.avatarUrl : pic" size="140">
				</u-avatar>
			</view>
			<view class="u-flex-1">
				<view class="u-font-18 u-p-b-20">
					{{ userInfo.nickName&&userDetail.userName ? userInfo.nickName+' ( '+userDetail.userName+' )' : '登录' }}
				</view>
				<view class="u-font-14 u-tips-color">
					{{ userCompany.deptName ? userCompany.deptName : '更多信息请在登录后查看噢' }}
				</view>
			</view>
			<view class="u-m-l-10 u-p-10">
				<u-icon v-show="!isLogin" name="arrow-right" color="#969799" size="28"></u-icon>
			</view>
		</view>

		<view class="u-m-t-20">
			<u-cell-group>
				<u-cell-item icon="rmb-circle" title="今日应收" :arrow="false">
					{{myTotal.inCome?'￥'+$options.filters["formatMoney"](myTotal.inCome)+'元':'暂无'}}
				</u-cell-item>
				<u-cell-item icon="rmb-circle-fill" title="今日应付" :arrow="false">
					{{myTotal.sumPay?'￥'+$options.filters["formatMoney"](myTotal.sumPay)+'元':'暂无'}}
				</u-cell-item>
				<u-cell-item icon="rmb" title="服务费率" :arrow="false">
					{{myTotal.expensePrice?myTotal.expensePrice*100+'%':'暂无'}}
				</u-cell-item>
			</u-cell-group>
		</view>

		<view class="u-m-t-20">
			<u-cell-group>
				<u-cell-item icon="coupon" title="用户协议" @click="toAgreement"></u-cell-item>
				<u-cell-item icon="coupon" title="隐私条款" @click="toPrivacy"></u-cell-item>
			</u-cell-group>
		</view>

		<view class="u-m-t-20">
			<u-cell-group>
				<u-cell-item v-show="isLogin" :arrow="false" title="退出登录" @click="logout"></u-cell-item>
			</u-cell-group>
		</view>
	</view>
</template>

<script>
	import {
		getUserinfo,
		getSum
	} from '../../api/getlist.js'
	export default {
		data() {
			return {
				pic: "/static/profile.png",
				isLogin: false,
				userInfo: {},
				userDetail: {},
				userCompany: {},
				myTotal: {}
			}
		},
		onLoad(e) {
			this.checkLoginStatus()
			this.getlist()
		},
		onPullDownRefresh() {
			this.getlist()
			uni.stopPullDownRefresh()
		},
		methods: {
			toAgreement() {
				uni.navigateTo({
					url: '/pages/center/agreement'
				})
			},
			toPrivacy() {
				uni.navigateTo({
					url: '/pages/center/privacy'
				})
			},
			getlist() {
				if (uni.getStorageSync('token')) {
					getSum().then(res => {
						if (res.data)
							this.myTotal = res.data
					})
				}
			},
			toLogin() {
				if (!this.isLogin) {
					uni.navigateTo({
						url: '/pages/login/index'
					})
				}
			},
			getUserinfoList() {
				getUserinfo().then(res => {
					if (res.user) {
						this.userDetail = res.user
						this.userCompany = this.userDetail.dept
						this.isLogin = true
					} else {
						this.isLogin = false
					}
				})
			},
			checkLoginStatus() {
				if (uni.getStorageSync('token')) {
					this.userInfo = uni.getStorageSync('userInfo')
					this.getUserinfoList()
				}
			},
			reset() {
				this.userDetail = {}
				this.userCompany = {}
				this.userInfo = {}
			},
			logout() {
				uni.showModal({
					title: '提示',
					content: '确定退出登录吗',
					success: (res) => {
						if (res.confirm) {
							this.isLogin = false
							uni.clearStorage();
							this.reset()
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ededed;
	}

	.camera {
		width: 54px;
		height: 44px;

		&:active {
			background-color: #ededed;
		}
	}

	.user-box {
		background-color: #fff;
	}
</style>
